<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div>
      <h1>生成器Generator</h1>
      <p>1.避免异步编程中回调嵌套过深</p>
      <p>2.生成器函数会自动帮我们生成一个生成器对象,需要调用next方法执行</p>
      <p>
        3.执行过程中遇到yield关键字,函数的执行就会被暂停,需要再次调用next才会再次执行
      </p>
    </div>
    <script>
      function* foo() {
        console.log("xjq");
        return 100;
      }

      const result = foo();
      console.log(result); //打印一个生成器对象
      console.log(result.next()); //{value:100,done:true}

      function* foo2() {
        console.log("111111");
        yield 100;
        console.log("222222");
        yield 200;
        console.log("333333");
        yield 300;
      }

      const result2 = foo2();
      console.log(result2.next());
      //11111
      //{value:100,done:false}
      console.log(result2.next());
      //222222
      //{value:200,done:false}
      console.log(result2.next());
      //333333
      //{value:300,done:false}
    </script>
  </body>
</html>
